package com.yurun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yurun.entity.Article;
import com.yurun.exception.NotFoundException;
import com.yurun.exception.PersistenceException;
import com.yurun.mapper.ArticleMapper;
import com.yurun.model.vo.CategoryArticleCount;
import com.yurun.model.vo.PageResult;
import com.yurun.model.vo.article.ArticleInfo;
import com.yurun.model.vo.article.ArticleInfoArticleshow;
import com.yurun.model.vo.article.ArticleInfoHomeshow;
import com.yurun.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    // 前台首页文章 9条
    private static final int firstPageSize=9;
    // 前台第二级 10条
    private static final int secondPageSize=10;
    // 后台每页显示10条
    private static final int pageSize=10;
    // 文章排序方式
    private static final String orderBy="is_top desc, create_time desc";


    @Override
    public void addArticle(Article article){
        // 增加文章
        if(articleMapper.addArticle(article)!=1){
            throw new PersistenceException("添加文章失败");
        }
    }

    @Override
    public void deleteArticleById(Long id){
        // 删除文章 -- 通过 ID
        if(articleMapper.deleteArticleById(id)!=1){
            throw new PersistenceException("删除文章失败");
        }
    }

    @Override
    public void updateArticleTopById(Long id,Boolean isTop){
        // 更新文章 -- 置顶状态 -- 通过 ID
        if(articleMapper.updateArticleTopById(id,isTop)!=1){
            throw new PersistenceException("更新文章顶置状态失败");
        }
    }

    @Override
    public void updateArticleViewsById(Long id,Integer views){
        // 更新文章 -- 浏览量 -- 通过ID
        if(articleMapper.updateArticleViewsById(id,views)!=1){
            throw new PersistenceException("更新文章浏览量失败");
        }
    }

    @Override
    public void updateArticle(Article article){
        // 更新文章
        if(articleMapper.updateArticle(article)!=1){
            throw new PersistenceException("更新文章失败");
        }
    }

    @Override
    public PageResult<ArticleInfo> getAllArticleInfoList(Integer pageNum){
        // 查询文章 -- 后台【文章管理页面】
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<ArticleInfo> articleInfoHomeshows=articleMapper.getAllArticleInfoList();
        PageInfo<ArticleInfo> pageInfo=new PageInfo<>(articleInfoHomeshows);
        PageResult<ArticleInfo> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public PageResult<ArticleInfo> getArticleInfoListByCategory(Integer category_id,Integer pageNum){
        // 查询文章 -- 后台【文章管理页面】 -- 通过 分类
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<ArticleInfo> articleInfoHomeshows=articleMapper.getArticleInfoListByCategory(category_id);
        PageInfo<ArticleInfo> pageInfo=new PageInfo<>(articleInfoHomeshows);
        PageResult<ArticleInfo> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public PageResult<ArticleInfo> getArticleInfoListByTitle(String title,Integer pageNum){
        // 查询文章 -- 后台【文章管理页面】 -- 通过 标题
        PageHelper.startPage(pageNum,pageSize,orderBy);
        List<ArticleInfo> articleInfoHomeshows=articleMapper.getArticleInfoListByTitle(title);
        PageInfo<ArticleInfo> pageInfo=new PageInfo<>(articleInfoHomeshows);
        PageResult<ArticleInfo> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public PageResult<ArticleInfoHomeshow> getArticleInfoHomeshowList(Integer pageNum){
        // 查询文章 -- 第一级页面
        PageHelper.startPage(pageNum,firstPageSize,orderBy);
        List<ArticleInfoHomeshow> articleInfoHomeshows=articleMapper.getArticleInfoHomeshowList();
        PageInfo<ArticleInfoHomeshow> pageInfo=new PageInfo<>(articleInfoHomeshows);
        PageResult<ArticleInfoHomeshow> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public PageResult<ArticleInfoArticleshow> getArticleInfoArticleshowList(Integer pageNum){
        // 查询文章 -- 第二级页面
        PageHelper.startPage(pageNum,secondPageSize,orderBy);
        List<ArticleInfoArticleshow> articleInfoArticleshows=articleMapper.getArticleInfoArticleshowList();
        PageInfo<ArticleInfoArticleshow> pageInfo=new PageInfo<>(articleInfoArticleshows);
        PageResult<ArticleInfoArticleshow> pageResult=new PageResult<>(pageInfo.getPages(),
                pageInfo.getList());
        return pageResult;
    }

    @Override
    public List<String> getGroupYearMonthList(){
        // 查询文章 -- 年月 列表
        return articleMapper.getGroupYearMonthList();
    }

    @Override
    public List<ArticleInfoArticleshow> getArticleListByCategory(Integer category_id){
        // 查询文章 -- 第二级页面 -- 通过 分类
        return articleMapper.getArticleListByCategory(category_id);
    }

    @Override
    public Article getArticleById(Integer id){
        // 查询文章 -- 获取单篇文章内容 -- 通过 ID
        Article article=articleMapper.getArticleById(id);
        if(article==null){
            throw new NotFoundException("文章不存在");
        }
        return article;
    }

    @Override
    public int getCountOfArticle(){
        // 查询文章 -- 文章总数
        return articleMapper.getCountOfArticle();
    }

    @Override
    public int getCountOfArticleByCategoryId(Integer categoryId){
        // 查询文章 -- 每个分类下文章总数 -- 通过 分类
        return articleMapper.getCountOfArticleByCategoryId(categoryId);
    }

    @Override
    public List<CategoryArticleCount> getCountListOfArticleAboutCategory(){
        // 查询文章 -- 获取每个分类 名称 下的文章数量
        return articleMapper.getCountListOfArticleAboutCategory();
    }
}
